<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/11/13
 * Time: 10:33
 */

namespace app\common\controller;


use app\common\model\RougeGetmoney;
use app\common\model\RougeSystemWx;
use Weixinpay\Company;
use Weixinpay\Wxtk;

class SendCashM
{



    public function is_hb_fk($uniacid,$id){
        $RougeSystemWx = new RougeSystemWx();
        $sysinfo = $RougeSystemWx->where('uniacid',$uniacid)->field('is_put_mode')->find();
        if ($sysinfo['is_put_mode'] == 1) {
            $res = $this->sendfk($id);
        } else {
            $res = $this->sendhb($id);
        }
        //END
        $map['id'] = $id;
        $RougeGetmoney = new RougeGetmoney();
        $datas['wxinfo']=json_encode($res,JSON_UNESCAPED_UNICODE);
        $RougeGetmoney->save($datas,$map);
        return $res;
    }



    //自动提现
    private function sendhb($id='')
    {
        $RougeGetmoney = new RougeGetmoney();
        $map['id'] = $id;
        $getinfo = $RougeGetmoney->where($map)->find();
        if ($getinfo['statu'] == 1 && $getinfo['uniacid'] == 1) {
            $RougeSystemWx = new RougeSystemWx();
            $mab['uniacid'] = $getinfo['uniacid'];
            $wxinfo = $RougeSystemWx->where($mab)->find();
            $weObj = new Wxtk();
            $notice = $weObj->generateNonceStr();
            $weObj->setParameter('nonce_str', $notice);
            $weObj->setParameter('mch_billno', $getinfo['orderid']);
            $weObj->setParameter('mch_id', $wxinfo['mch_id']);
            $weObj->setParameter('wxappid', $wxinfo['appid']);
            $weObj->setParameter('send_name', $wxinfo['title']);
            $weObj->setParameter('re_openid',  $getinfo['openid']);
            $weObj->setParameter('total_amount', $getinfo['price']*100);
            $weObj->setParameter('total_num', 1);
            $weObj->setParameter('wishing', $wxinfo['wishing']);
            $weObj->setParameter('client_ip', $_SERVER['REMOTE_ADDR']);
            $weObj->setParameter('act_name', $wxinfo['title']);
            $weObj->setParameter('remark', $wxinfo['wishing']);
            $sss = $weObj->create_refund_xml($wxinfo['partnerkey']);
            $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
            $responseXml = $weObj->curl_post_ssl($url, $sss, $wxinfo['apiclient_cert'], $wxinfo['apiclient_key']);
            $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $responseArr = ( array )$responseObj;
            if ($responseArr) {
                if ($responseArr['return_code'] == 'SUCCESS' && $responseArr['result_code'] == "SUCCESS") {
                    if ($responseArr['return_msg'] == '发放成功') {
                        $datas['statu']=2;
                        $datas['wxinfo']=json_encode($responseArr,JSON_UNESCAPED_UNICODE);
                        $RougeGetmoney->save($datas,$map);
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['result_code'] = "SUCCESS";
                        $rarr['return_msg'] = $responseArr['return_msg'];
                        $rarr['result_msg'] = $responseArr['return_msg'];
                    } else {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['result_code'] = "FAIL";
                        $rarr['return_msg'] = $responseArr['return_msg'];
                        $rarr['result_msg'] = $responseArr['return_msg'];
                    }
                } else {
                    if (isset($responseArr['err_code_des'])) {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['result_code'] = "FAIL";
                        $rarr['return_msg'] = $responseArr['err_code_des'];
                        $rarr['result_msg'] = $responseArr['err_code_des'];

                    } elseif (isset($responseArr['return_msg'])) {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['result_code'] = "FAIL";
                        $rarr['return_msg'] = $responseArr['return_msg'];
                        $rarr['result_msg'] = $responseArr['return_msg'];
                    } else {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['result_code'] = "FAIL";
                        $rarr['return_msg'] = "未知错误";
                        $rarr['result_msg'] = "未知错误";
                    }
                }
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['result_code'] = "FAIL";
                $rarr['return_msg'] = "证书参数有误";
                $rarr['result_msg'] = "证书参数有误";
            }
        }else{
            $rarr['return_code'] = "SUCCESS";
            $rarr['result_code'] = "FAIL";
            $rarr['return_msg'] = "订单状态异常";
            $rarr['result_msg'] = "订单状态异常";
        }
        return $rarr;
    }


    private function sendfk($id = ''){
        $RougeGetmoney = new RougeGetmoney();
        $map['id'] = $id;
        $getinfo = $RougeGetmoney->where($map)->find();
        $RougeSystemWx = new RougeSystemWx();
        $Syswxinfo = $RougeSystemWx->where('uniacid',1)->find();
        $mch_appid = $Syswxinfo['appid'];
        $mchid = $Syswxinfo['mch_id'];
        $partener_trade_no = $getinfo['orderid'];
        $openid = $getinfo['openid'];
        $desc = $Syswxinfo['wishing'];
        $partnerkey = $Syswxinfo['partnerkey'];
        $apiclient_cert = $Syswxinfo['apiclient_cert'];
        $apiclient_key = $Syswxinfo['apiclient_key'];
        if ($getinfo['statu'] == 1 && $getinfo['uniacid'] == 1) {
            $weObj = new Company();
            $notice = $weObj->generateNonceStr();
            $weObj->setParameter('mch_appid', $mch_appid);//商户appid
            $weObj->setParameter('mchid', $mchid);//商户号
            $weObj->setParameter('nonce_str', $notice);//随机字符串
            $weObj->setParameter('partner_trade_no',  $partener_trade_no);//订单号
            $weObj->setParameter('openid',$openid );//用户openID
            $weObj->setParameter('check_name', "NO_CHECK");//校验姓名（默认不校验）
            $weObj->setParameter('amount', $getinfo['price']*100);//付款金额
            $weObj->setParameter('desc',$desc);//付款备注
            $weObj->setParameter('spbill_create_ip', $_SERVER['REMOTE_ADDR']);//发起IP
            $sss = $weObj->create_refund_xml($partnerkey);//签名
            $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
            $responseXml = $weObj->curl_post_ssl($url, $sss, $apiclient_cert, $apiclient_key);
            $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $responseArr = ( array )$responseObj;
//            var_dump($responseArr);
//            file_put_contents('/www/wwwroot/h5yiszhcom/sendfk.txt', 'e-'.$id.':' . json_encode($responseArr) . PHP_EOL, FILE_APPEND);
            if ($responseArr) {
                if ($responseArr['return_code'] == 'SUCCESS' && $responseArr['result_code'] == "SUCCESS") {

                    $datas['statu']=2;
                    $datas['wxinfo']=json_encode($responseArr,JSON_UNESCAPED_UNICODE);
                    $RougeGetmoney->save($datas,$map);
                    $rarr['return_code'] = "SUCCESS";
                    $rarr['result_code'] = "SUCCESS";
                    $rarr['return_msg'] = $responseArr['return_msg'];
                    $rarr['result_msg'] = $responseArr['return_msg'];

                } else {
                    if (isset($responseArr['err_code_des'])) {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['result_code'] = "FAIL";
                        $rarr['return_msg'] = $responseArr['err_code_des'];
                        $rarr['result_msg'] = $responseArr['err_code_des'];

                    } elseif (isset($responseArr['return_msg'])) {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['result_code'] = "FAIL";
                        $rarr['return_msg'] = $responseArr['return_msg'];
                        $rarr['result_msg'] = $responseArr['return_msg'];
                    } else {
                        $rarr['return_code'] = "SUCCESS";
                        $rarr['result_code'] = "FAIL";
                        $rarr['return_msg'] = "未知错误";
                        $rarr['result_msg'] = "未知错误";
                    }
                }
            } else {
                $rarr['return_code'] = "SUCCESS";
                $rarr['result_code'] = "FAIL";
                $rarr['return_msg'] = "证书参数有误";
                $rarr['result_msg'] = "证书参数有误";
            }
//            $rarr = $responseArr;
        }else{
            $rarr['return_code'] = "SUCCESS";
            $rarr['result_code'] = "FAIL";
            $rarr['return_msg'] = "订单状态异常";
            $rarr['result_msg'] = "订单状态异常";
        }

//        file_put_contents('/www/wwwroot/h5yiszhcom/sendfk-r.txt', 'e-'.$id.':' . json_encode($rarr) . PHP_EOL, FILE_APPEND);
        return $rarr;
    }

}